//
//  XGNewFeatureView.m
//  XGNewFeature
//
//  Created by 小果 on 2017/2/25.
//  Copyright © 2017年 小果. All rights reserved.
//

#import "XGNewFeatureView.h"
#import "Masonry.h"

#define  marginRight -20
#define  marginBottom -80
@interface XGNewFeatureView ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIScrollView *scroll;
@property (nonatomic, weak) UIPageControl *pageControl;
@end
@implementation XGNewFeatureView

- (void)dealloc {
    NSLog(@"%s   %d     %s",__FUNCTION__,__LINE__,__FILE__);
}

#pragma mark - 构造函数
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    return self;
}

#pragma mark - 设置数据
- (void)setImageNames:(NSArray *)imageNames {
    _imageNames = imageNames;
    
    CGRect rect = self.bounds;
    NSInteger index = 0;
    
    for (NSString *iconName in _imageNames) {
        UIImage *image = [UIImage imageNamed:iconName];
        
        CGFloat offsetX = index * rect.size.width;
        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectOffset(rect, offsetX, 0)];
        imageV.image = image;
        
        [_scroll addSubview:imageV];
        index++;
    }
    CGFloat svWidth = (imageNames.count + 1) * rect.size.width;
    _scroll.contentSize = CGSizeMake(svWidth, 0);
    
    [self addMoreButton];
    
    // 设置分页控件页数和当前页码
    _pageControl.numberOfPages = _imageNames.count;
    _pageControl.currentPage = 0;
}

#pragma mark - 添加更多按钮
- (void)addMoreButton {
    
    for (UIView *v in _scroll.subviews) {
        
        UIButton *moreBtn = [[UIButton alloc] init];
        
        UIImage *normalImage = [UIImage imageNamed:@"common_more_black"];
        [moreBtn setImage:normalImage forState:UIControlStateNormal];
        
        UIImage *lightImage = [UIImage imageNamed:@"common_more_white"];
        [moreBtn setImage:lightImage forState:UIControlStateHighlighted];
        
        [moreBtn sizeToFit];
        // 设置控件 V 的用户交互的原因是：因为 scroll 中添加的是一个默认不支持用户交互的 imageView，如果将 btn 添加在上面，要想让 btn 的点击事件有响应，需要设置它的用户交互为 YES
        v.userInteractionEnabled = YES;
        [v addSubview:moreBtn];
        
        [moreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(v).offset(marginRight);
            make.bottom.equalTo(v).offset(marginBottom);
            make.size.mas_equalTo(CGSizeMake(78, 40));
        }];
        
        [moreBtn addTarget:self action:@selector(clickMoreBtnWithChangeView:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - 更多按钮的响应方法
- (void)clickMoreBtnWithChangeView:(UIButton *)btn {
    UIView *v = btn.superview;
    
    CGFloat scale = 2.0;
    [UIView animateWithDuration:1.0 animations:^{
        
        v.transform = CGAffineTransformMakeScale(scale, scale);
        v.alpha = 0.0;
        
    } completion:^(BOOL finished) {
        // 新特性视图的使命已经结束，整个从视图上删除，将用户的交互直接交还给控制器！
        [self removeFromSuperview];
    }];
}

#pragma mark - UIScrollViewDelegate
// 只要滚动视图发生变化就会被调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger pageNumber = (NSInteger)(offsetX / scrollView.bounds.size.width + 0.5);
    
    _pageControl.currentPage = pageNumber;
    
    if (pageNumber == _imageNames.count) {
        _pageControl.hidden = YES;
    } else {
        _pageControl.hidden = NO;
    }
}
// 滚动视图完全停止滚动会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger pageNumber = offsetX / scrollView.bounds.size.width;
    
    _pageControl.currentPage = pageNumber;
    
    if (pageNumber == _imageNames.count) {
        [self removeFromSuperview];
    }
}

#pragma mark - 设置新特性界面
- (void)setupUI {
    
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
    scroll.pagingEnabled = YES;
    scroll.bounces = NO;
    
    scroll.showsVerticalScrollIndicator = NO;
    scroll.showsHorizontalScrollIndicator = NO;
    
    scroll.delegate = self;
    [self addSubview:scroll];
    _scroll = scroll;
    
    /************添加分页控件*************/
    UIPageControl *pageCon = [[UIPageControl alloc] init];
    pageCon.currentPageIndicatorTintColor = [UIColor yellowColor];
    pageCon.pageIndicatorTintColor = [UIColor blackColor];
    pageCon.userInteractionEnabled = NO;
    
    [self addSubview:pageCon];
    
    [pageCon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.bottom.equalTo(self).offset(marginBottom);
    }];
    _pageControl = pageCon;
}
@end
